package com.my.screenadapter.mThread;

import android.util.Log;
import android.view.View;

import com.my.screenadapter.Base.BaseActivity;
import com.my.screenadapter.R;

/**
 *
 * 结果：一个【空】while无限不Sleep一直循环  占用8%左右的 CPU
 * Createdy by zyj 2019-1-18 13:35:16
 * Function : 测试 是否可以kill Thread
 * thread 停止 方式
 * thread 保活方式
 * 1、轮训查看保活
 *
 *
 * 结论kill thread id 失败，整进程被kill  进而重启。
 *
 * 线程停止方式 ：
 * 1、循环中标志位
 *
 * 2、thread.stop();
 * 但使用stop方法是很危险的，就象突然关闭计算机电源，而不是按正常程序关机一样，可能会产生不可预料的结果，因此，并不推荐使用stop方法来终止线程。
 * 测试：java.lang.IllegalStateException: Could not execute method for android:onClick。。。。    Caused by: java.lang.UnsupportedOperationException   at com.my.screenadapter.mThread.ThreadKillActivity.stopThread(ThreadKillActivity.java:72)
 *
 * 3、interrupt方法终止线程
         * public void run()
         * {try{
         * sleep(50000);  // 延迟50秒}
         * catch (InterruptedException e)
         * {System.out.println(e.getMessage());}}
         * public static void main(String[] args) throws Exception{
         * Thread thread = new ThreadInterrupt();
         * thread.start();
         * System.out.println("在50秒之内按任意键中断线程!");
         * System.in.read();
         * thread.interrupt();
         * thread.join();
         * System.out.println("线程已经退出!");
         * }
 *
 */
public class ThreadKillActivity extends BaseActivity {
    private static final String TAG = "ThreadKillActivity";
    private Thread thread;
    private boolean threadIsStopTag;
    private String  threadName = "Thread kill";
    private WatchDogCommonThread mWatchDogThread;

    @Override
    protected void init() {
        mWatchDogThread = new WatchDogCommonThread(threadName, 3 * 1000, new WatchDogCommonThread.WatchDogInterface() {
            @Override
            public void callNoThread(String str) {
                Log.w(TAG, "callNoThread: " + "restart...............");
                threadIsStopTag = false;
                startThread(null);
            }
        });
        mWatchDogThread.start();
    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_thread_kill;
    }

    public void startThread(View view) {
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true&&!threadIsStopTag) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i(TAG, "run: " + "Thread kill is run");
                }
            }
        }, threadName);
        thread.start();
        Log.i(TAG, "startThread: thread id=="+thread.getId());
    }

    public void stopThread(View view) {
        if (thread != null) {
            thread.stop();
        }
    }

    //通过标志位结束
    public void stopThreadTag(View view) {
        threadIsStopTag = true;
    }
}
